package com.ruoyi.am.controller;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.am.domain.AmDriving;
import com.ruoyi.framework.web.domain.server.Sys;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.am.domain.OutData;
import com.ruoyi.am.service.IOutDataService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出车信息Controller
 * 
 * @author hkkk
 * @date 2020-11-23
 */
@Controller
@RequestMapping("/am/data")
public class OutDataController extends BaseController
{
    private String prefix = "am/data";

    @Autowired
    private IOutDataService outDataService;

    @RequiresPermissions("am:data:view")
    @GetMapping()
    public String data()
    {
        return prefix + "/data";
    }

    /**
     * 查询出车信息列表
     */
    @RequiresPermissions("am:data:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OutData outData)
    {
        startPage();
        List<OutData> list = outDataService.selectOutDataList(outData);
        return getDataTable(list);
    }

    /**
     * 导出出车信息列表
     */
    @RequiresPermissions("am:data:export")
    @Log(title = "出车信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OutData outData)
    {
        List<OutData> list = outDataService.selectOutDataList(outData);
        ExcelUtil<OutData> util = new ExcelUtil<OutData>(OutData.class);
        return util.exportExcel(list, "data");
    }

    /**
     * 新增出车信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存出车信息
     */
    @RequiresPermissions("am:data:add")
    @Log(title = "出车信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OutData outData)
    {
        return toAjax(outDataService.insertOutData(outData));
    }

    /**
     * 修改出车信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OutData outData = outDataService.selectOutDataById(id);
        mmap.put("outData", outData);
        return prefix + "/edit";
    }

    /**
     * 修改保存出车信息
     */
    @RequiresPermissions("am:data:edit")
    @Log(title = "出车信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OutData outData)
    {
        return toAjax(outDataService.updateOutData(outData));
    }

    /**
     * 删除出车信息
     */
    @RequiresPermissions("am:data:remove")
    @Log(title = "出车信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(outDataService.deleteOutDataByIds(ids));
    }

    @ResponseBody
    @PostMapping("/addData")
    public AjaxResult addData(@RequestParam String obj_data){
        JSONObject object=JSONObject.parseObject(obj_data);
        AmDriving amDriving=JSONObject.toJavaObject(object, AmDriving.class);
        System.out.println(amDriving);
        System.out.println(object);
        OutData outData=new OutData();
        outData.setCity(object.getString("city"));
        outData.setBeforCheck(object.getString("beforCheck"));
        outData.setTimes(object.getString("times"));
        outData.setAfterCheck(object.getString("afterCheck"));
        outData.setProblem(object.getString("problem"));
        outData.setAdvice(object.getString("advice"));
        outData.setTrvel(object.getString("trvel"));
        outData.setPeople(object.getString("people"));
        outData.setOutTime(new Date(object.getString("outTime")));
        outData.setMileage(object.getString("mileage"));
        outData.setCarNum(object.getString("car_num"));
        return toAjax(outDataService.insertOutData(outData));
    }

}
